package com.emm.yixun.website.controller.system;

//框架包
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.emm.yixun.common.model.TelRecord;
import com.emm.yixun.common.model.User;
import com.emm.yixun.common.model.dto.TelRecordDto;
import com.emm.yixun.common.page.Page;
import com.emm.yixun.common.page.PageUtil;
import com.emm.yixun.common.response.Response;
import com.emm.yixun.common.utils.DateStyle;
import com.emm.yixun.common.utils.DateUtils;
import com.emm.yixun.system.service.TelRecordServiceFacade;
import com.emm.yixun.website.common.ajax.AjaxRes;
import com.emm.yixun.website.common.utils.base.Const;
import com.emm.yixun.website.common.utils.security.ShiroUtil;
import com.emm.yixun.website.controller.base.BaseController;
//common
//website
//变化的引用

@Controller
@RequestMapping("/system/telRecord/")
public class TelRecordController extends BaseController<TelRecord> {

	@Autowired
	private TelRecordServiceFacade telRecordServiceFacade;

	@RequestMapping(value = "index")
	public String index(ModelMap map) {
		if (doSecurityIntercept(Const.RESOURCES_TYPE_MENU)) {
			// 查询菜单按钮权限
			map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));
			return "/system/telRecord/list";
		}
		return Const.NO_AUTHORIZED_URL;
	}

	/**
	 * ui优化版 功能描述: <br>
	 * 〈功能详细描述〉
	 * 
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping(value = "getIndex", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes getIndex() {
		AjaxRes ar = getAjaxRes();
		Map<String, Object> map = new HashMap<String, Object>();
		// 查询菜单按钮权限
		map.put("permitBtn", getPermitBtn(Const.RESOURCES_TYPE_FUNCTION));
		ar.setSucceed(map);
		return ar;
	}

	// 新增返回实体对象
	@RequestMapping(value = "add", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes add(TelRecordDto dto) {
		AjaxRes ar = getAjaxRes();

		/*
		 * 1.判断权限2.调用新增接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "system:telRecord:add"))) {
			try {
				User user = ShiroUtil.getCurrentUser();
				dto.setMerchantId(user.getMerchantId());
				dto.setCreateUserId(user.getId());
				dto.setDelFlag("0");
				dto.setUpdateTime(new Date());
				Response<TelRecordDto> response = telRecordServiceFacade.save(dto);
				if (response.isSuccess()) {
					ar.setSucceedMsg(Const.SAVE_SUCCEED);
				} else {
					ar.setFailMsg(Const.SAVE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.SAVE_FAIL);
			}
		}
		return ar;
	}

	// 查看
	@RequestMapping(value = "view", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes view(Long id) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用查看接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "system:telRecord:view"))) {
			try {
				Response<TelRecord> response = telRecordServiceFacade.findById(id);
				if (response.isSuccess()) {
					ar.setSucceed(response.getEntity());
				} else {
					ar.setFailMsg(Const.DATA_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DATA_FAIL);
			}
		}
		return ar;
	}

	// 修改
	@RequestMapping(value = "edit", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes edit(TelRecordDto dto) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用修改接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "system:telRecord:edit"))) {
			try {
				Response<Integer> response = telRecordServiceFacade.updateByPrimaryKey(dto, true);
				if (response.isSuccess()) {
					ar.setSucceedMsg(Const.UPDATE_SUCCEED);
				} else {
					ar.setFailMsg(Const.UPDATE_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.UPDATE_FAIL);
			}
		}
		return ar;
	}

	// 删除
	@RequestMapping(value = "delete", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes delete(Long id) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用查看接口3.返回信息
		 */
		if (ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON, "system:telRecord:delete"))) {
			try {
				Response<Integer> response = telRecordServiceFacade.deleteByPrimaryKey(id);
				if (response.isSuccess()) {
					ar.setSucceedMsg(Const.DEL_SUCCEED);
				} else {
					ar.setFailMsg(Const.DEL_FAIL);
				}
			} catch (Exception e) {
				logger.error(e.toString(), e);
				ar.setFailMsg(Const.DEL_FAIL);
			}
		}
		return ar;
	}

	// 分页查询
	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes list(TelRecordDto dto, String callTimeBeforeStr, String callTimeAfterStr, Integer page, Integer limit) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用分页查询接口3.返回信息
		 */
		// if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"system:telRecord:list"))){
		try {
			User user = ShiroUtil.getCurrentUser();
			dto.setMerchantId(user.getMerchantId());
			if (StringUtils.isNotBlank(callTimeBeforeStr)) {
				callTimeBeforeStr = callTimeBeforeStr + " 00:00:00";
				dto.setCallTimeBefore(DateUtils.StringToDate(callTimeBeforeStr, DateStyle.YYYY_MM_DD_HH_MM_SS));
			}
			if (StringUtils.isNotBlank(callTimeAfterStr)) {
				callTimeAfterStr = callTimeAfterStr + " 23:59:59";
				dto.setCallTimeAfter(DateUtils.StringToDate(callTimeAfterStr, DateStyle.YYYY_MM_DD_HH_MM_SS));
			}
			if (StringUtils.isNotBlank(dto.getCustomerName())) {
				dto.setCustomerName("%" + dto.getCustomerName() + "%");
			}
			dto.setOrders(" t.create_time desc ");
			Page<TelRecordDto> response = telRecordServiceFacade.findMultiPageByDto(dto, PageUtil.begin(page, limit),
					limit);
			if (response.getSuccess()) {
				if (null == response.getRows())
					response.setRows(new ArrayList<TelRecordDto>());
				response.setPageNumber(page);
				ar.setSucceed(response);
			} else {
				ar.setFailMsg(Const.DATA_FAIL);
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		// }
		return ar;
	}

	/**
	 * ui优化 功能描述: <br>
	 * 〈功能详细描述〉
	 * 
	 * @param dto
	 * @param callTimeBeforeStr
	 * @param callTimeAfterStr
	 * @param pageSize
	 * @param pageNum
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@RequestMapping(value = "getList", method = RequestMethod.POST)
	@ResponseBody
	public AjaxRes getList(TelRecordDto dto, String callTimeBeforeStr, String callTimeAfterStr, Integer pageSize,
			Integer pageNum) {
		AjaxRes ar = getAjaxRes();
		/*
		 * 1.判断权限2.调用分页查询接口3.返回信息
		 */
		// if(ar.setNoAuth(doSecurityIntercept(Const.RESOURCES_TYPE_BUTTON,"system:telRecord:list"))){
		try {
			User user = ShiroUtil.getCurrentUser();
			dto.setMerchantId(user.getMerchantId());
			if (StringUtils.isNotBlank(callTimeBeforeStr)) {
				callTimeBeforeStr = callTimeBeforeStr + " 00:00:00";
				dto.setCallTimeBefore(DateUtils.StringToDate(callTimeBeforeStr, DateStyle.YYYY_MM_DD_HH_MM_SS));
			}
			if (StringUtils.isNotBlank(callTimeAfterStr)) {
				callTimeAfterStr = callTimeAfterStr + " 23:59:59";
				dto.setCallTimeAfter(DateUtils.StringToDate(callTimeAfterStr, DateStyle.YYYY_MM_DD_HH_MM_SS));
			}
			if (StringUtils.isNotBlank(dto.getCustomerName())) {
				dto.setCustomerName("%" + dto.getCustomerName() + "%");
			}
			dto.setOrders(" t.create_time desc ");
			Page<TelRecordDto> response = telRecordServiceFacade.findMultiPageByDto(dto,
					PageUtil.begin(pageNum, pageSize), pageSize);
			if (response.getSuccess()) {
				if (null == response.getRows())
					response.setRows(new ArrayList<TelRecordDto>());
				response.setPageNumber(pageNum);
				ar.setSucceed(response);
			} else {
				ar.setFailMsg(Const.DATA_FAIL);
			}
		} catch (Exception e) {
			logger.error(e.toString(), e);
			ar.setFailMsg(Const.DATA_FAIL);
		}
		// }
		return ar;
	}
}
